﻿using System.Windows;
using Wpf3D.ViewModels;
using Wpf3D.WcfServiceIIS;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Media;
using System;
using System.Globalization;

namespace Wpf3D.Views
{
    /// <summary>
    /// Interaction logic for LotterHistory.xaml
    /// </summary>
    public partial class LotterHistory : Window
    {
        static Pagenation page = new Pagenation() { Count = 100, PageIndex = 2, PageSize = 20 };
        Service1Client client = new Service1Client();
        LotterHistoryViewModel vm = new LotterHistoryViewModel();

        public LotterHistory()
        {
            InitializeComponent();
            DataBind();
            for (int i = 1; i <= page.Count / page.PageSize; i++)
            {
                this.combPageList.Items.Add(new ComboBoxItem { Content = i.ToString() });
            }

            this.combPageList.SelectedIndex = page.PageIndex-1;

        }

        private void btnFrist_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button)
            {
                var btn = sender as Button;

                switch (btn.Name)
                {
                    case "btnFirst":
                        page.PageIndex = 1;
                        break;
                    case "btnPrevious":
                        {
                            if (page.PageIndex > 1)
                            {
                                page.PageIndex -= 1;
                            }

                            break;
                        }
                    case "btnNext":
                        {
                            if (page.PageIndex < page.Count / page.PageSize)
                            {
                                page.PageIndex += 1;
                            }

                            break;
                        }
                    case "btnLast":
                        page.PageIndex = page.Count / page.PageSize;
                        break;
                    default:
                        break;
                }
            }
            else if (sender is ComboBox)
            {
                var comb = sender as ComboBox;
                page.PageIndex = comb.SelectedIndex + 1;
            }

            DataBind();
        }

        private void DataBind()
        {
            vm.lotterCollection = client.GetLotterNumberCollection(ref page).lotterNumberCollection;
            dataGridHistory.ItemsSource = vm.lotterCollection;
            dataGridHistory.Items.Refresh();
        }
    }
}
